package com.css.dzj.zfjg.modules.feedbackrelate.Service.Impl;


import com.alibaba.druid.util.StringUtils;
import com.css.dzj.zfjg.modules.admforce.entity.JgJgxwForceActionEntity;
import com.css.dzj.zfjg.modules.admforce.service.JgJgxwForceActionService;
import com.css.dzj.zfjg.modules.adminspection.entity.JgJgxwCheckActionEntity;
import com.css.dzj.zfjg.modules.adminspection.service.JgJgxwCheckActionService;
import com.css.dzj.zfjg.modules.admpunishment.entity.JgJgxwPunishActionEntity;
import com.css.dzj.zfjg.modules.admpunishment.service.JgJgxwPunishActionService;
import com.css.dzj.zfjg.modules.feedbackrelate.Service.JgLhjgRwfkglService;
import com.css.dzj.zfjg.modules.feedbackrelate.entity.JgLhjgRwfkglEntity;
import com.css.dzj.zfjg.modules.feedbackrelate.entity.QJgLhjgRwfkglEntity;
import com.css.dzj.zfjg.modules.feedbackrelate.mapper.LhjgRwfkglMapper;
import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.jcdx.entity.QJgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.other.entity.JgJgxwOtherActionEntity;
import com.css.dzj.zfjg.modules.other.service.JgJgxwOtherActionService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("jgLhjgRwfkglService")
public class JgLhjgRwfkglServiceImpl  implements JgLhjgRwfkglService {
    @Autowired
    private LhjgRwfkglMapper lhjgRwfkglMapper;

    @Autowired
    private JgJgxwCheckActionService jgJgxwCheckActionService;

    @Autowired
    private JgJgxwPunishActionService jgJgxwPunishActionService;

    @Autowired
    private JgJgxwForceActionService jgJgxwForceActionService;

    @Autowired
    private JgJgxwOtherActionService jgJgxwOtherActionService;

    //注入JPAQueryFactory工厂
    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Override
    public QueryResults <JgLhjgRwfkglEntity> queryPage(Pageable pageable) {
        QJgLhjgRwfkglEntity q = QJgLhjgRwfkglEntity.jgLhjgRwfkglEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        return  jpaQueryFactory
                .selectFrom(q)
                .where(
                        predicate
                )
                .offset(pageable.getOffset())   //起始页
                .limit(pageable.getPageSize())  //每页大小
                .fetchResults();
    }


    /**
     * 根据任务编码查询监管行为
     * @param taskNo
     * @return
     */
    @Override
    public List<JgLhjgRwfkglEntity> findListByTaskNo(String taskNo,String superviseItemCode) {
        QJgLhjgRwfkglEntity q = QJgLhjgRwfkglEntity.jgLhjgRwfkglEntity;
        Predicate predicate=q.isNotNull().or(q.isNull());
        predicate =  ExpressionUtils.and(predicate, q.taskno.eq(taskNo));
        predicate =  ExpressionUtils.and(predicate, q.superviseItemCode.eq(superviseItemCode));
        List<JgLhjgRwfkglEntity> list = jpaQueryFactory
                .selectFrom(q)
                .where(predicate)
               /*/.orderBy(
                    // 根据创建日期，降序排序
                )*/
               .fetch();
        return list;
    }

    @Override
    public JgLhjgRwfkglEntity getById(String taskno) {
        QJgLhjgRwfkglEntity q = QJgLhjgRwfkglEntity.jgLhjgRwfkglEntity;
        return  jpaQueryFactory
                .selectFrom(q)
                .where(q.taskno.eq(taskno)
                )
                .fetchOne();
    }

    @Override
    public String save(JgLhjgRwfkglEntity jgLhjgRwfkgl) {
        lhjgRwfkglMapper.save(jgLhjgRwfkgl);
        return null;
    }
    /**
     * 根据任务编码查询监管行为
     * @param taskNo
     * @return
     */
    @Override
    public JgLhjgRwfkglEntity findOneByTaskNo(String taskNo,String superviseItemCode,String uuid) {
        QJgLhjgRwfkglEntity q = QJgLhjgRwfkglEntity.jgLhjgRwfkglEntity;
        Predicate predicate=q.isNotNull().or(q.isNull());
        predicate =  ExpressionUtils.and(predicate, q.taskno.eq( taskNo));
        predicate =  ExpressionUtils.and(predicate, q.superviseItemCode.eq(superviseItemCode));
        predicate =  ExpressionUtils.and(predicate, q.dffkId.eq(uuid));
        JgLhjgRwfkglEntity jgLhjgRwfkglEntitie = jpaQueryFactory
                .selectFrom(q)
                .where(predicate)
                .fetchOne();
        return jgLhjgRwfkglEntitie;
    }

    /**
     * 通过任务编码删除反馈关联和行为信息
     * @param taskNo
     */
    @Override
    public void deleteFkglAndActionByTaskNo(String taskNo) {
        // 查询所有检查行为信息的反馈关联表
        List<JgLhjgRwfkglEntity> checkFkglList = findListByTaskNo(taskNo, "1");
        if(checkFkglList != null && checkFkglList.size() > 0) {
            for (JgLhjgRwfkglEntity fkglEntity : checkFkglList) {
                if(fkglEntity != null && !StringUtils.isEmpty(fkglEntity.getActionId())) {
                    JgJgxwCheckActionEntity checkActionEntity = jgJgxwCheckActionService.findById(fkglEntity.getActionId());
                    if(checkActionEntity != null) {
                        // 物理删除检查行为信息
                        jgJgxwCheckActionService.physicalDelete(checkActionEntity);
                    }
                }
                delete(fkglEntity);
            }
        }
        List<JgLhjgRwfkglEntity> punishFkglList = findListByTaskNo(taskNo, "2");
        if(punishFkglList != null && punishFkglList.size() > 0) {
            for(JgLhjgRwfkglEntity fkglEntity : punishFkglList) {
                if(fkglEntity != null && !StringUtils.isEmpty(fkglEntity.getActionId())) {
                    JgJgxwPunishActionEntity punishActionEntity = jgJgxwPunishActionService.findById(fkglEntity.getActionId());
                    if(punishActionEntity != null) {
                        jgJgxwPunishActionService.physicalDelete(punishActionEntity);
                    }
                }
                delete(fkglEntity);
            }
        }
        List<JgLhjgRwfkglEntity> forceFkglList = findListByTaskNo(taskNo, "3");
        if(forceFkglList != null && forceFkglList.size() > 0) {
            for(JgLhjgRwfkglEntity fkglEntity : forceFkglList) {
                if(fkglEntity != null && !StringUtils.isEmpty(fkglEntity.getActionId())) {
                    JgJgxwForceActionEntity forceActionEntity = jgJgxwForceActionService.findById(fkglEntity.getActionId());
                    if(forceActionEntity != null) {
                        jgJgxwForceActionService.physicalDelete(forceActionEntity);
                    }
                }
                delete(fkglEntity);
            }
        }
        List<JgLhjgRwfkglEntity> otherFkglList = findListByTaskNo(taskNo, "4");
        if(otherFkglList != null && otherFkglList.size() > 0) {
            for(JgLhjgRwfkglEntity fkglEntity : otherFkglList) {
                if(fkglEntity != null && !StringUtils.isEmpty(fkglEntity.getActionId())) {
                    JgJgxwOtherActionEntity otherActionEntity = jgJgxwOtherActionService.findById(fkglEntity.getActionId());
                    if(otherActionEntity != null) {
                        jgJgxwOtherActionService.physicalDelete(otherActionEntity);
                    }
                }
                delete(fkglEntity);
            }
        }

    }


    /**
     * 物理删除反馈关联表
     * @param fkglEntity
     */
    public void delete(JgLhjgRwfkglEntity fkglEntity) {
        try{
            lhjgRwfkglMapper.delete(fkglEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询所有任务关联信息
     * @return 任务关联信息List
     */
    @Override
    public List queryAllRwsfkgl() {
        QJgLhjgRwfkglEntity q = QJgLhjgRwfkglEntity.jgLhjgRwfkglEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        List<JgLhjgRwfkglEntity> list = jpaQueryFactory.selectFrom(q)
                .where(predicate)
                .orderBy(q.uuid.desc())
                .fetch();
        return list;
    }

}